package com.arvato.minipro.controller.admin.user;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.arvato.minipro.bean.bean_do.user.EcUserDo;
import com.arvato.minipro.bean.bean_dto.common.PageData;
import com.arvato.minipro.bean.bean_dto.common.Query;
import com.arvato.minipro.bean.bean_dto.common.Result;
import com.arvato.minipro.bean.bean_vo.user.EcUserVo;
import com.arvato.common.util.EmojiFilter;
import com.arvato.minipro.service.user.EcUserService;

/**
 * 处理小程序用户相关的Controller
 * @author ZHAN655
 *
 */
@Controller
@RequestMapping(value="/user")
public class EcUserController {

	@Autowired
	private EcUserService userService;
	
	/**
	 * 获取当前用户
	 * @param req
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/getCurrUser",method=RequestMethod.POST)
	public Result getCurrUser(HttpServletRequest req) {
		EcUserVo user = (EcUserVo)req.getSession().getAttribute("loginUser");
		Result result = new Result();
		result.setData(user);
		return result;
	}
	/**
	 * 根据条件查询多个用户
	 * @param param
	 * param 中的参数 ：start
	 *  				limit
	 *  			或者keyword
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/list",method=RequestMethod.POST)
	public PageData listUserByQuery(@RequestParam Map<String, Object> param){
		Query query = new Query(param);
		PageData data = userService.listUserByQuery(query);
		return data;
	}
	/**
	 * 查询一个电商小程序用户
	 * @param param
	 * param
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/detail",method=RequestMethod.POST)
	public Result getUserById(String userId){
		Result result = new Result(true);
		EcUserVo userVo = userService.getUserById(userId);
		result.setData(userVo);
		return result;
	}
	/**
	 * 更新电商小程序用户信息
	 * @param user
	 * @return
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	@ResponseBody 
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public Result updateBack(EcUserDo user, HttpServletRequest req) throws IllegalArgumentException, IllegalAccessException
	{
		Result result = new Result(false);
		try{
			String nickName = user.getNickname();
			if(nickName != null && !nickName.equals("") && EmojiFilter.containsEmoji(nickName)) {
				// 如果要是修改昵称 那么先验证下昵称是否包含表情符号，有的话就去掉
				user.setNickname(EmojiFilter.filterEmoji(nickName));
			}
			int row = userService.update(user);
			if(row == 1){
				result.setResult(true);
			}
		}catch (Exception e) {
			e.printStackTrace();
		}
		// 无论成功不成功 返回修改后的信息
		EcUserVo userVo = userService.getUserById(user.getUserId());
		result.setData(userVo);
		return result;
	}
	/**
	 * 删除电商小程序用户
	 * @param userIds
	 * @return
	 */
	@ResponseBody 
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public Result delete(HttpServletRequest req, String[] userIds){
		Result result = new Result(true);
		EcUserVo operUser = new EcUserVo();
		EcUserDo user = new EcUserDo();
		user.setStatus(4);
		user.setLastoperType(7);
		user.setLastoperDate(new Date());
		if(operUser != null){
			user.setLastoperUser(operUser.getUserId());
		}
		try{
			for(String userId : userIds){
				user.setUserId(userId);
				userService.delete(user);
			}
		}catch (Exception e) {
			e.printStackTrace();
			result.setResult(false);
		}
		return result;
	}

}
